package com.example.mybatispdemo.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatispdemo.store.entity.Product_supplierEntity;
import com.example.mybatispdemo.store.dao.Product_supplierDao;
import com.example.mybatispdemo.store.service.IProduct_supplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ljp
 * @since 2022-10-31
 */
@Service
public class Product_supplierServiceImpl extends ServiceImpl<Product_supplierDao, Product_supplierEntity> implements IProduct_supplierService {

    @Override
    public Product_supplierEntity addItem(int productId, int supplierId) {
        if(this.getEntity(productId,supplierId)!=null){
            return null;
        }
        Product_supplierEntity entity = new Product_supplierEntity();
        entity.setProductId(productId);
        entity.setSupplierId(supplierId);
        this.save(entity);
        return this.getEntity(productId,supplierId);
    }

    @Override
    public List<Product_supplierEntity> getByProductId(int productId) {
        QueryWrapper<Product_supplierEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Product_supplierEntity::getProductId,productId);
        return this.list(queryWrapper);
    }

    @Override
    public List<Product_supplierEntity> getBySupplierId(int supplierId) {
        QueryWrapper<Product_supplierEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Product_supplierEntity::getSupplierId,supplierId);
        return this.list(queryWrapper);
    }

    @Override
    public Product_supplierEntity getEntity(int productId, int supplierId) {
        QueryWrapper<Product_supplierEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Product_supplierEntity::getProductId,productId).eq(Product_supplierEntity::getSupplierId,supplierId);
        return this.getOne(queryWrapper);
    }

    @Override
    public void deleteByProductId(int productId) {
        QueryWrapper<Product_supplierEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Product_supplierEntity::getProductId,productId);
        this.baseMapper.delete(queryWrapper);
    }

    @Override
    public void deleteBySupplierId(int supplierId) {
        QueryWrapper<Product_supplierEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Product_supplierEntity::getSupplierId,supplierId);
        this.baseMapper.delete(queryWrapper);
    }


}
